﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using EC_Agent.Models;
using EC_Agent.AgentClass;
using System.Web.Security;

namespace EC_Agent
{
    public partial class UpdatePersonalInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int year;
            for (year = DateTime.Now.Year - 50; year <= DateTime.Now.Year; year++)
            {
                ddlYearUp.Items.Add(year.ToString());
            }
            if (!IsPostBack)
            {
                if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
                {
                    //MessageBox.Show(Session["Username"].ToString());
                    LabWelcome.Visible = true;
                    LabUser.Text = Session["Username"].ToString();
                    LabUser.Visible = true;
                    DangNhap.Attributes.Add("style", "display:none");
                    DangKy.Attributes.Add("style", "display:none");
                    string key = Session["Username"].ToString();

                    DataSet ds = new KhachHangModels().ListKhachHang();
                    DataRow[] dw = ds.Tables[0].Select(" Username ='" + key + "'");
                    hidMaKH.Value = dw[0]["MaKhachHang"].ToString();
                    labUsernameUp.Text = dw[0]["Username"].ToString();
                    txtTenKHUp.Text = dw[0]["TenKH"].ToString();
                    txtDiaChiUp.Text = dw[0]["DiaChi"].ToString();
                    txtEmailUp.Text = dw[0]["Email"].ToString();
                    txtDienThoaiUp.Text = dw[0]["DienThoai"].ToString();
                    DateTime dt = Convert.ToDateTime(dw[0]["NgaySinh"].ToString());
                    ddlDayUp.Text = dt.Day.ToString();
                    ddlMonthUp.SelectedValue = dt.Month.ToString();
                    ddlYearUp.Text = dt.Year.ToString();
                    txtSoTheTinDungUp.Text = dw[0]["SoTheTinDung"].ToString();
                }
            }
        }

        protected void btnClick_Update(object sender, EventArgs e)
        {
            KhachHangInfo KhachHang = new KhachHangInfo();

            DateTime NgaySinh = new DateTime(Convert.ToInt16(ddlYearUp.Text), Convert.ToInt16(ddlMonthUp.SelectedValue), Convert.ToInt16(ddlDayUp.Text));
            string passmahoa;
            if (txtPasswordUp.Text != "")
            {
                passmahoa = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPasswordUp.Text.Trim(), "MD5");
            }
            else
            {
                passmahoa = "";
            }
            KhachHang.MaKhachHang = Int16.Parse(hidMaKH.Value);
            KhachHang.TenKH = txtTenKHUp.Text;
            KhachHang.DiaChi = txtDiaChiUp.Text;
            KhachHang.Email = txtEmailUp.Text;
            KhachHang.DienThoai = txtDienThoaiUp.Text;
            KhachHang.NgaySinh = Convert.ToDateTime(NgaySinh);
            KhachHang.SoTheTinDung = txtSoTheTinDungUp.Text;
            KhachHang.Username = labUsernameUp.Text;
            KhachHang.Password = passmahoa;
            KhachHang.TinhTrang = Convert.ToBoolean(true);
            KhachHangModels KH = new KhachHangModels();
            int temp = KH.UpdateKH(KhachHang);
            if (temp == 1)
            {
                MessageBox.Show("Bạn đã cập nhật thành công");
            }
            else
            {
                MessageBox.Show("Bạn đã cập nhật không thành công");
            }
        }
    }
}